package com.cacho.s2b.lesson.learn;

import com.cacho.s2b.lesson.utils.EncryptHelper;

import java.io.File;
import java.nio.charset.StandardCharsets;

import java.util.Base64;

/**
 * Description: java.util.Base64
 **/

public class LearnBase64 {
    private static final String blankText = "将要加密的数据";
    private static final String encodeText = "5bCG6KaB5Yqg5a+G55qE5pWw5o2u";

    public static String encode(String blankText) {
        return Base64.getEncoder().encodeToString(blankText.getBytes(StandardCharsets.UTF_8));
    }

    public static String encode(byte[] blankByte) {
        return Base64.getEncoder().encodeToString(blankByte);
    }

    public static String decode(String encodeText) {
        byte[] byte1 = Base64.getDecoder().decode(encodeText);
        return new String(byte1, StandardCharsets.UTF_8);
    }

    public static String decode(byte[] encodeByte) {
        return new String(encodeByte, StandardCharsets.UTF_8);
    }

    public static String encodeFile(String filePath) {
        System.out.println(String.format("Base64转换后的String：\n%s", EncryptHelper.encode(new File(filePath))));
        return EncryptHelper.encode(new File(filePath));
    }

    public static void main(String[] args) {
        //System.out.println(encode(blankText));
        //System.out.println(decode(encodeText));
        encodeFile("H:\\wei\\OpenSSL-Win64\\bin\\test\\java.pfx");
    }
}
